import { airspaceMapServer, airspaceListServer } from '@/api'
import _turf from '@/utils/turfUtil'
import { createCircle, calculateCentroid } from '@/utils/common'
//文旅观光
const getAir = (val) => {
  const newVal = []
  const newcoordinates = []
  val?.coordinate?.forEach((item) => {
    newVal.push(item.split(','))
  })
  newVal.forEach((item) => {
    newcoordinates.push(_turf.transform([Number(item[0]), Number(item[1]), Number(val.topHeight)]))
  })
  return newcoordinates
}

// 获取需要添加电子围栏的片区
export const addCultureAirSpace = async () => {
  let res01 = await airspaceMapServer()
  const list = res01.data.filter((item) => item.airspaceType?.includes('文旅观光'))
  const airSpaceNames = list.map((i) => i.airspaceName)

  let res02 = await airspaceListServer()
  const data = res02.data.filter((item) => airSpaceNames.includes(item.airspaceName))
  const newdata = data.map((item) => ({
    ...item,
    coordinate: item.pointsGps?.split('_')
  }))

  const polygonArr = []
  newdata.forEach((item, index) => {
    const coordinate =
      item.geometryType === 2
        ? getAir(item)
        : createCircle(
            _turf.transform([Number(item.centLng), Number(item.centLat)]),
            item.centRadius
          )

    const opolygon = {
      id: 'cltureAirSpace-' + index, //3DPolygon唯一标识id
      groupId: 'cltureAirSpace',
      coordinates: coordinate, //构成3DPolygon的坐标点数组
      //   color: '#00a7eb', //3DPolygon颜色
      color: '#008000', //3DPolygon颜色
      height: 1000, //3D多边形的高度
      intensity: 1.0, //亮度
      style: 0, //3DPolygon的样式 请参照API开发文档选取枚举
      generateTop: false, //是否生成顶面
      generateSide: true, //是否生成侧面
      generateBottom: false //是否生成底面
    }
    polygonArr.push(opolygon)
  })
  //批量添加3DPolygon
  await __g.polygon3d.add(polygonArr)
}

export function clearCultureAirSpace() {
  __g.polygon3d.clear(null)
}
